#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

#define MAX_SIZE 100

int height(char tree[], int index, int n) 
{
	if (index >= n || tree[index] == '0') 
		return 0;
	int leftHeight = height(tree, 2 * index + 1, n);
	int rightHeight = height(tree, 2 * index + 2, n);
	return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}

void postOrder(char tree[], int index, int n) 
{
	if (index >= n || tree[index] == '0')
		return;
	postOrder(tree, 2 * index + 1, n);
	postOrder(tree, 2 * index + 2, n);
	printf("%c %d\n", tree[index], height(tree, 2 * index + 1, n) - height(tree, 2 * index + 2, n));
}


void test4()
{
	int t, n;
	scanf("%d", &t);

	while (t--) {
		scanf("%d", &n);
		char tree[MAX_SIZE];
		for (int i = 0; i < n; i++)
		{
			scanf(" %c", &tree[i]);
		}
		postOrder(tree, 0, n);
	}
}

int main()
{
	test4();
	return 0;
}
